//验证码登录 - 输入验证码
let api = require("../../server/Api.js")
let app = getApp();
let _user = require("../../utils/User.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobileNo: "",
    code: "获取验证码",
    curTime: 60,
    isDisabled: false,
    isError3: false,
    msgId: "",
    invitationCode: "",
    inviteCodeDisabled: false
  },

  onLoad: function(options) {
    let that = this;
    let mobileNo = options.mobileNo;
    that.setData({
      mobileNo: mobileNo
    })
    //获取验证码
    that.getVerfyCode();
    if (app.globalData.shareUserId && app.globalData.shareUserId != "") {
      api.getConsumerById(app.globalData.shareUserId).then(res => {
        if (res.inviteCode) {
          that.setData({
            invitationCode: res.inviteCode,
            inviteCodeDisabled: true
          })
        }
      })
    }
  },
  submit: function(e) {
    let that = this;
    let code = e.detail.value.code
    let msgId = this.data.msgId;
    let invitationCode = e.detail.value.invitationCode;
    if (!msgId) {
      tt.showModal({
        title: '提示',
        content: '请获取短信验证码',
      })
      return;
    }

    if (!code) {
      tt.showModal({
        title: '提示',
        content: '请输入短信验证码',
      })
      return;
    }
    // if (!invitationCode) {
    //   tt.showModal({
    //     title: '提示',
    //     content: '请输入邀请码',
    //   })
    //   return;
    // }
    if (invitationCode != "" && invitationCode.length != 7) {
      app.showToast("邀请码必须是七位数!")
      return;
    }
    let params = {
      code: code,
      companyId: app.globalData.companyId,
      platformId: app.globalData.platformId,
      invitationCode: invitationCode,
      mobile: that.data.mobileNo,
      msgId: msgId
    }

    //登录请求
    app.showLoading();
    api.loginByCode(params).then(res => {
      //登录成功
      tt.hideLoading()
      app.globalData.userBean = res;
      _user.saveUser(res); //本地保存
      app.backHome();
    }).catch(msg => {
      tt.hideLoading()
      let message = typeof msg == "string" ? msg : msg.message;
      app.showToast(message);
    })


  },
  //获取验证码
  getVerfyCode: function() {
    if (this.data.isDisabled) {
      return
    }
    let that = this;
    app.showLoading();
    api.getVerifyCode(this.data.mobileNo).then(res => {
      tt.hideLoading()
      console.log(res);
      if (res) {
        that.setData({
          msgId: res
        })
        that.startDownTime();
      }
    }).catch(msg => {
      tt.hideLoading()
      var errMsg = "";
      if (typeof msg == 'string') {
        errMsg = msg;
      } else {
        errMsg = msg.errMsg;
      }
      tt.showModal({
        title: '提示',
        content: errMsg,
      })
    })
  },
  //开始倒计时
  startDownTime: function() {
    let that = this;
    var timer = setInterval(function() {
      var curTime = that.data.curTime;
      curTime--;
      that.setData({
        curTime: curTime,
        code: '获取验证码(' + curTime + '秒)',
        isDisabled: true
      })
      if (that.data.curTime <= 0) {
        clearInterval(timer)
        that.setData({
          code: '获取验证码',
          curTime: 60,
          isDisabled: false
        })
      }
    }, 1000)
  }






})